﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Recognition;

namespace CodeTalk.CTSpeechCommands
{
    class CorrectionCommand : SpeechCommand
    {
        internal override GrammarBuilder GrammarBld(Context context, Context[] libraries)
        {
            Choices ch = new Choices();
            for (int i = 1; i <= 10; i++)
                ch.Add(new SemanticResultValue(i.ToString(), i));

            GrammarBuilder gb = new GrammarBuilder("correction");
            gb.Append(new SemanticResultKey("number", ch));
            return gb;
        }

        public override string Name
        {
            get { return "Correction command"; }
        }

        public override string Description
        {
            get { return Name; }
        }

        internal override bool IsUndoable
        {
            get { return false; }
        }

        internal override bool CanSpokenFormMatch(string spokenForm)
        {
            if (spokenForm.StartsWith("correction "))
            {
                for (int i = 1; i <= 10; i++)
                {
                    if (spokenForm == "correction " + i)
                        return true;
                }
            }
            return false;
        }

        internal override object Execute(SpeechMode callingMode, string recognizedText, SemanticValue semValue)
        {
            try
            {
                if (!ActionManager.Correction((int)semValue["number"].Value))
                    callingMode.CurrentGuiHandler.SetErrorFeedback("Cannot correct command!");
                return null;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
